/*
 * Filename:    EvaluationOrgDao.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-14
 */
package com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.constants.EvaluationOrgConstants;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvalMarkDeatilInfoDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvalMarkInfoDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvalMarkUserComfirmDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvaluationMarkContentDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvaluationPointCutInfo;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.IntegrityEvalDTO;
import com.paic.pafa.biz.dao.BaseDAO;

@Component("evaluationOrgDao")
public class EvaluationOrgDao extends BaseDAO {

	public IntegrityEvalDTO loadAuditiCaseDTOByCode(IntegrityEvalDTO bean) {

		IntegrityEvalDTO queryBean = (IntegrityEvalDTO) this._queryForObject("evaluationIllegal.loadIntgrityEvalInfo",
				bean);
		return queryBean;
	}

	/**
	 * 添加诚信考评子表信息
	 * 
	 * @param bean
	 */
	public void addIntgrityEvalChild(EvalMarkUserComfirmDTO bean) {
		this._insert("evaluationIllegal.addIntgrityEvalChild", bean);
	}

	/**
	 * 根据id修改对应的诚信考评子表信息
	 * 
	 * @param bean
	 */
	public void updateIntgrityEvalChild(EvalMarkUserComfirmDTO bean) {
		this._update("evaluationIllegal.updateIntgrityEvalChild", bean);
	}

	/**
	 * 根据诚信考评id及考评规则id查询对应的诚信考评子表信息是否存在
	 * 
	 * @param bean
	 */
	public String queryIntgrityEvalChild(EvalMarkUserComfirmDTO bean) {
		return this._getString("evaluationIllegal.queryIntgrityEvalChild", bean);
	}

	/**
	 * 添加诚信考评扣分确认人信息
	 * 
	 * @param bean
	 */
	public void addEvalMarkUser(EvalMarkDeatilInfoDTO bean) {
		this._insert("evaluationIllegal.addEvalMarkUser", bean);
	}

	/**
	 * 根据id修改对应的诚信考评扣分确认人信息
	 * 
	 * @param bean
	 */
	public void updateEvalMarkUser(EvalMarkDeatilInfoDTO bean) {
		this._update("evaluationIllegal.updateEvalMarkUser", bean);
	}

	/**
	 * 根据扣分值id查询扣分确认人是否存在
	 * 
	 * @param bean
	 */
	public int queryEvalMarkUser(EvalMarkDeatilInfoDTO bean) {
		return this._getInt("evaluationIllegal.queryEvalMarkUser", bean);
	}

	public void addUpdateEvalMarkUser(EvalMarkDeatilInfoDTO bean) {
		int count = this.queryEvalMarkUser(bean);
		if (count > 0) {
			if (StringUtils.hasText(bean.getEvaluationUser())) {
				this.updateEvalMarkUser(bean);
			} else {
				bean.setIsValid(EvaluationOrgConstants.IS_NOT_VALID);
				this.updateEvalMarkUser(bean);
			}
		} else if (StringUtils.hasText(bean.getEvaluationUser())) {
			this.addEvalMarkUser(bean);
		}
	}

	/**
	 * 添加诚信考评扣分确认信息
	 * 
	 * @param bean
	 */
	public void addEvalIntegriyUser(EvalMarkInfoDTO bean) {
		this._insert("evaluationIllegal.addEvalIntegriyUser", bean);
	}

	/**
	 * 根据扣分值id修改对应的诚信考评扣分确认信息
	 * 
	 * @param bean
	 */
	public void updateEvalIntegriyUser(EvalMarkInfoDTO bean) {
		this._update("evaluationIllegal.updateEvalIntegriyUser", bean);
	}

	/**
	 * 根据扣分值id查询诚信考评扣分确认信息
	 * 
	 * @param bean
	 */
	public int queryEvalIntegriyUser(EvalMarkInfoDTO bean) {
		return this._getInt("evaluationIllegal.queryEvalIntegriyUser", bean);
	}

	public void addUpdateEvalIntegriyUser(EvalMarkInfoDTO bean) {
		int count = this.queryEvalIntegriyUser(bean);
		if (count > 0) {
			if (StringUtils.hasText(bean.getIsConfirm())) {
				this.updateEvalIntegriyUser(bean);
			} else {
				bean.setIsValid(EvaluationOrgConstants.IS_NOT_VALID);
				this.updateEvalIntegriyUser(bean);
			}
		} else if (StringUtils.hasText(bean.getIsConfirm())) {
			this.addEvalIntegriyUser(bean);
		}
	}

	/**
	 * 考评子表id获取对应的考评分值
	 * 
	 * @param bean
	 */
	public String getCountScore(String id) {
		return this._getString("evaluationIllegal.getCountScore", id);
	}

	/**
	 * 考评表id获取对应的各分组类考评详细信息
	 * 
	 * @param bean
	 */
	public List<EvalMarkUserComfirmDTO> getEvalMarkUserComfirmInfo(String evaluationCode) {
		return (List<EvalMarkUserComfirmDTO>) this
				._list("evaluationIllegal.getEvalMarkUserComfirmList", evaluationCode);
	}

	// -------------------------------------------
	/**
	 * 根据考评规则ID 加载对应的考核内容
	 * 
	 * @param categoryId
	 * @return
	 */
	public List<EvaluationMarkContentDTO> queryEvaluationContentDTO(String id) {
		return (List<EvaluationMarkContentDTO>) this._list("evaluationIllegal.queryEvaluationRuleContent", id);
	}
	
	
  public EvaluationPointCutInfo queryEvaluationPointCutInfoWarInfo(String warNo){
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("warNo", warNo);
		List<EvaluationPointCutInfo> list=(List<EvaluationPointCutInfo>) this._queryForList("evaluationIllegal.queryWarningByWarnCode", map);
		
		if(list!=null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
		
	}
	
	
	public EvaluationPointCutInfo queryEvaluationPointCutInfoAuditCaseInfo(String caseCode){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("caseCode", caseCode);
		List<EvaluationPointCutInfo> list=(List<EvaluationPointCutInfo>) this._queryForList("evaluationIllegal.queryAuditCaseByCaseCode", map);
		
		if(list!=null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 加载考评的扣分值详情
	 * @param orgCode
	 * @param standardScoreId
	 * @param targetType
	 * @return
	 */
	public List<EvaluationPointCutInfo> queryPointValue(String orgCode,String standardScoreId,String targetType){
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("orgCode", orgCode);
		map.put("standardScoreId", standardScoreId);
		map.put("targetType", targetType);
		return (List<EvaluationPointCutInfo>) this._list("evaluationIllegal.queryPointValue",map);
		
	}

	/**
	 * 根据考核内容id加载对应的细则
	 * 
	 * @param contentId
	 * @return
	 */
	public List<EvalMarkDeatilInfoDTO> queryEvaluationMarkRuleDTO(Map<String, Object> map) {
		return (List<EvalMarkDeatilInfoDTO>) this._list("evaluationIllegal.queryEvaluationMarkRule", map);
	}

	/**
	 * 根据考核细则id 加载对应的扣分值信息
	 * 
	 * @param markInfoId
	 * @return
	 */
	public List<EvalMarkInfoDTO> queryEvaluationMarkValueDTO(Map<String, Object> map) {
		return (List<EvalMarkInfoDTO>) this._list("evaluationIllegal.queryEvaluationMarkValues", map);
	}

	/**
	 * 考评表id获取对应的考评子表（状态为暂存的条数）
	 * 
	 * @param bean
	 */
	public Integer getCountEvaluation(String id) {
		return this._getInt("evaluationIllegal.getCountEvaluation", id);
	}

	/**
	 * 修改诚信考评主表的状态
	 */
	public void updateIntgrityEvalStatus(String status, String updatedBy, String evaluationCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", status);
		map.put("updatedBy", updatedBy);
		map.put("evaluationCode", evaluationCode);
		this._update("evaluationIllegal.updateIntgrityEvalStatus", map);
	}

	/**
	 * 根据扣分值表ID和诚信考评字表主键删除对应的扣分确认表信息
	 * 
	 * @param evalChildId
	 * @param markValueId
	 */
	public void deleteIntegrityMarkByChildId(String evalChildId, String markValueId) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("evalChildId", evalChildId);
		map.put("markValudId", markValueId);
		this._delete("evaluationIllegal.deleteEvalIntegriyMark", map);

	}

	/**
	 * <!-- 根据扣分值表ID和诚信考评字表主键删除对应的扣分确认人表信息 -->
	 * 
	 * @param evalChildId
	 * @param markValueId
	 */
	public void deleteIntegrityMarkUserInfoByChildId(String evalChildId, String markValueId) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("evalChildId", evalChildId);
		map.put("markValudId", markValueId);
		this._delete("evaluationIllegal.deleteEvalIntegriyMarkUser", map);

	}

	public long queryEvalChildRecord(String evaluationCode, String status) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("evaluationCode", evaluationCode);
		map.put("status", status);
		long count = this._queryForCount("evaluationIllegal.getEvalChildRecord", map);

		return count;

	}

	// ------------------------------------------------------------------

	/**
	 * 根据诚信考评子表主键id查找关联诚信考评主表考评编码
	 * 
	 * @param evalChildId
	 * @return
	 */
	public String findByChildId(String evalChildId) {
		return this._getString("evaluationIllegal.getCoodId", evalChildId);
	}

	/**
	 * 根据考评编码修改考评总分
	 * 
	 * @param coodId
	 * @param score
	 * @return
	 */
	public void updateEvaluationSumScroe(String coodId, String score) {
		Map<String, String> ma = new HashMap<String, String>();
		ma.put("coodId", coodId);
		ma.put("score", score);
		this._update("evaluationIllegal.getTotalScore", ma);
	}

	/**
	 * 初始化考评得分
	 * 
	 * @param evaluationCode
	 */
	public void updateInitScroe(String childId, String evaluationCode, String evaluationScore) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("evaluationCode", evaluationCode);
		map.put("evaluationScore", evaluationScore);
		map.put("childId", childId);
		this._update("evaluationIllegal.updateInitScroe", map);
	}

	// ------------------------------------------------------------------

	public String findOrgCodeByEvalChildId(String evalChildId) {
		String orgCode = "";
		Map<String, String> map = new HashMap<String, String>();
		map.put("evalChildId", evalChildId);

		List<String> list = (List<String>) this._queryForList("evaluationIllegal.findOrgCodeByEvalChildId", map);

		if (list != null && list.size() > 0) {
			orgCode = list.get(0);
		}
		return orgCode;
	}

	public void resetIntgrityReviewInfo(String evaluationCode,
			String statusthree,String userName) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("evaluationCode", evaluationCode);
		map.put("status", statusthree);
		map.put("userName", userName);
		this._update("evaluationIllegal.resetIntgrityReviewInfo", map);
	}

}
